
#include "msg_update.h"
#include "page_base.h"
#include "img_common.h"
#include "sys_scene.h"
#include "pt_ipc.h"
#include "pt_info.h"
#include "pt_dev.h"
#include "pt_update.h"
#include "sys_font.h"
#include "pt_fs.h"


static ui_msg_update_t  ui_msg_update;


static  char * icon_msg_esc[1] = 
{
    "Close.png",
};

static int msg_update_clicked_event(void)
{
    debug_d("msg update\n");
    return 0;
}

static void msg_update_event_callback(lv_event_t* event)
{
    if (event->code == LV_EVENT_CLICKED)
    {
        msg_update_clicked_event();
    }
}

static int msg_update_close_clicked_event(void)
{
    debug_d("msg update close\n");

    pop_win_unload();

    return 0;
}

static void msg_update_close_event_callback(lv_event_t* event)
{
    if (event->code == LV_EVENT_CLICKED)
    {
        msg_update_close_clicked_event();
    }
}



int ui_msg_update_create(lv_obj_t *superview)
{
    char txt_buff[256];

    ui_msg_update.buff = malloc(4096);
    if(!ui_msg_update.buff){
        debug_e("malloc err\n");
        return -1;
    }

    ui_msg_update.bg_esc = lv_obj_create(superview);

    lv_obj_set_pos(ui_msg_update.bg_esc,440,5);
    lv_obj_set_size(ui_msg_update.bg_esc,32,32);

	lv_obj_set_style_radius(ui_msg_update.bg_esc, 5, LV_PART_MAIN);
	lv_obj_set_style_border_width(ui_msg_update.bg_esc, 0, LV_PART_MAIN);
	lv_obj_set_style_border_color(ui_msg_update.bg_esc, lv_color_make(0x00, 0x00, 0x00), LV_PART_MAIN);
	lv_obj_set_style_border_opa(ui_msg_update.bg_esc, LV_OPA_0, LV_PART_MAIN);

	lv_obj_set_style_outline_width(ui_msg_update.bg_esc, 0, LV_PART_MAIN);
	lv_obj_set_style_shadow_width(ui_msg_update.bg_esc, 0, LV_PART_MAIN);
	lv_obj_set_scrollbar_mode(ui_msg_update.bg_esc, LV_SCROLLBAR_MODE_OFF);
	lv_obj_set_style_pad_all(ui_msg_update.bg_esc, 0,0);
    
    #if 1
    lv_obj_set_style_bg_opa(ui_msg_update.bg_esc,LV_OPA_0,0);
    #else
    lv_obj_set_style_bg_opa(ui_msg_update.bg_esc,LV_OPA_30,0);
    lv_obj_set_style_bg_color(ui_msg_update.bg_esc,lv_color_make(0,0,0),0);
    #endif 

    lv_obj_add_event_cb(ui_msg_update.bg_esc, msg_update_close_event_callback, LV_EVENT_ALL, NULL);
    //***************************************************
    ui_msg_update.img_esc = lv_img_create(ui_msg_update.bg_esc);
    lv_img_set_src(ui_msg_update.img_esc,pt_fs_get(icon_msg_esc[0]));
    lv_obj_set_pos(ui_msg_update.img_esc,0,0);
    //**********************************************************************************************
    ui_msg_update.title = lv_label_create(superview);
    lv_obj_set_pos(ui_msg_update.title,144,9);
    lv_obj_set_size(ui_msg_update.title,192,25);
    lv_obj_set_style_text_align(ui_msg_update.title,LV_TEXT_ALIGN_CENTER,0);
    lv_obj_set_style_text_opa(ui_msg_update.title,LV_OPA_100,LV_PART_MAIN);
    lv_obj_set_style_text_color(ui_msg_update.title,lv_color_make(0xff,0xff,0xff),LV_PART_MAIN); 
    lv_obj_set_style_text_font(ui_msg_update.title, lv_font_get(20), LV_STATE_DEFAULT);

    memset(txt_buff,0,sizeof(txt_buff));
    sprintf(txt_buff,"发现新版本%s",pt_version_get());

    lv_label_set_text( ui_msg_update.title, txt_buff);
    //**********************************************************************************************

    ui_msg_update.version = lv_label_create(superview);
    lv_obj_set_pos(ui_msg_update.version,21,52);
    lv_obj_set_size(ui_msg_update.version,192,25);
    lv_obj_set_style_text_align(ui_msg_update.version,LV_TEXT_ALIGN_LEFT,0);
    lv_obj_set_style_text_opa(ui_msg_update.version,LV_OPA_100,LV_PART_MAIN);
    lv_obj_set_style_text_color(ui_msg_update.version,lv_color_make(0x99,0x99,0x99),LV_PART_MAIN); 
    lv_obj_set_style_text_font(ui_msg_update.version, lv_font_get(20), LV_STATE_DEFAULT);

    memset(txt_buff,0,sizeof(txt_buff));
    sprintf(txt_buff,"更新内容：");

    lv_label_set_text( ui_msg_update.version, txt_buff);
    //**********************************************************************************************
    //soft version info

    ui_msg_update.bg_txt = lv_obj_create(superview);

    lv_obj_set_pos(ui_msg_update.bg_txt,21,87);
    lv_obj_set_size(ui_msg_update.bg_txt,437,144);

	lv_obj_set_style_radius(ui_msg_update.bg_txt, 5, LV_PART_MAIN);
	lv_obj_set_style_border_width(ui_msg_update.bg_txt, 0, LV_PART_MAIN);
	lv_obj_set_style_border_color(ui_msg_update.bg_txt, lv_color_make(0x00, 0x00, 0x00), LV_PART_MAIN);
	lv_obj_set_style_border_opa(ui_msg_update.bg_txt, LV_OPA_0, LV_PART_MAIN);

	lv_obj_set_style_outline_width(ui_msg_update.bg_txt, 0, LV_PART_MAIN);
	lv_obj_set_style_shadow_width(ui_msg_update.bg_txt, 0, LV_PART_MAIN);
	//lv_obj_set_scrollbar_mode(ui_msg_update.bg_txt, LV_SCROLLBAR_MODE_OFF);
	lv_obj_set_style_pad_all(ui_msg_update.bg_txt, 0,0);
    
    #if 1
    lv_obj_set_style_bg_opa(ui_msg_update.bg_txt,LV_OPA_0,0);
    #else
    lv_obj_set_style_bg_opa(ui_msg_update.bg_txt,LV_OPA_30,0);
    lv_obj_set_style_bg_color(ui_msg_update.bg_txt,lv_color_make(0,0,0),0);
    #endif 

    //txt

    ui_msg_update.txt = lv_label_create(ui_msg_update.bg_txt);
    lv_obj_set_pos(ui_msg_update.txt,0,0);
    //lv_obj_set_size(ui_msg_update.txt,241,25);
    lv_obj_set_style_text_align(ui_msg_update.txt,LV_TEXT_ALIGN_LEFT,0);
    lv_obj_set_style_text_opa(ui_msg_update.txt,LV_OPA_100,LV_PART_MAIN);
    lv_obj_set_style_text_color(ui_msg_update.txt,lv_color_make(0x99,0x99,0x99),LV_PART_MAIN); 
    lv_obj_set_style_text_font(ui_msg_update.txt, lv_font_get(18), LV_STATE_DEFAULT);

    lv_obj_set_width(ui_msg_update.txt, 420);
    lv_label_set_long_mode(ui_msg_update.txt, LV_LABEL_LONG_WRAP);

    #if 0
    lv_obj_set_style_bg_opa(ui_msg_update.txt,LV_OPA_0,0);
    #else
    lv_obj_set_style_bg_opa(ui_msg_update.txt,LV_OPA_30,0);
    lv_obj_set_style_bg_color(ui_msg_update.txt,lv_color_make(0,0,0),0);
    #endif 

    memset(ui_msg_update.buff,0,4096);
    snprintf(ui_msg_update.buff,4096,"更新内容，更新内容，更新内容，更新内容，\
    更新内容，更新内容，更新内容，更新内容，更新内容，更新内容，\
    更新内容，更新内容，更新内容，更新内容，更新内容，更新内容，\
    更新内容，更新内容，更新内容，更新内容，更新内容，更新内容，\
    更新内容，更新内容，更新内容，更新内容，");

    lv_label_set_text(ui_msg_update.txt, ui_msg_update.buff);
    //**********************************************************************************************
    //update btn

    ui_msg_update.bg_update = lv_obj_create(superview);

    lv_obj_set_pos(ui_msg_update.bg_update,52,250);
    lv_obj_set_size(ui_msg_update.bg_update,350,50);

	lv_obj_set_style_radius(ui_msg_update.bg_update, 25, LV_PART_MAIN);
	lv_obj_set_style_border_width(ui_msg_update.bg_update, 0, LV_PART_MAIN);
	lv_obj_set_style_border_color(ui_msg_update.bg_update, lv_color_make(0x00, 0x00, 0x00), LV_PART_MAIN);
	lv_obj_set_style_border_opa(ui_msg_update.bg_update, LV_OPA_0, LV_PART_MAIN);

	lv_obj_set_style_outline_width(ui_msg_update.bg_update, 0, LV_PART_MAIN);
	lv_obj_set_style_shadow_width(ui_msg_update.bg_update, 0, LV_PART_MAIN);
	lv_obj_set_scrollbar_mode(ui_msg_update.bg_update, LV_SCROLLBAR_MODE_OFF);
	lv_obj_set_style_pad_all(ui_msg_update.bg_update, 0,0);
    
    #if 0
    lv_obj_set_style_bg_opa(ui_msg_update.bg_update,LV_OPA_0,0);
    #else
    lv_obj_set_style_bg_opa(ui_msg_update.bg_update,LV_OPA_100,0);
    lv_obj_set_style_bg_color(ui_msg_update.bg_update,lv_color_make(0x26,0x7a,0xff),0);
    #endif 

    lv_obj_add_event_cb(ui_msg_update.bg_update, msg_update_event_callback, LV_EVENT_ALL, NULL);
    //**********************************************************************************************
    ui_msg_update.txt_update = lv_label_create(superview);
    lv_obj_set_pos(ui_msg_update.txt_update,115,260);
    lv_obj_set_size(ui_msg_update.txt_update,250,30);
    lv_obj_set_style_text_align(ui_msg_update.txt_update,LV_TEXT_ALIGN_CENTER,0);
    lv_obj_set_style_text_opa(ui_msg_update.txt_update,LV_OPA_100,LV_PART_MAIN);
    lv_obj_set_style_text_color(ui_msg_update.txt_update,lv_color_make(0xff,0xff,0xff),LV_PART_MAIN); 
    lv_obj_set_style_text_font(ui_msg_update.txt_update, lv_font_get(24), LV_STATE_DEFAULT);

    lv_label_set_text(ui_msg_update.txt_update,"立即更新");
    //**********************************************************************************************



    return 0;

}

int ui_msg_update_destroy(lv_obj_t *superview)
{
    if(ui_msg_update.buff){
        free(ui_msg_update.buff);
        ui_msg_update.buff = NULL;
    }


    return 0;
}




static void msg_update_on_custom_attr_config(page_base_t* self)
{


}
static void msg_update_on_view_load(page_base_t* self)
{


}
static void msg_update_on_view_did_load(page_base_t* self)
{
    ui_msg_update_create(self->root);

}
static void msg_update_on_view_will_appear(page_base_t* self)
{


}
static void msg_update_on_view_did_appear(page_base_t* self)
{


}
static void msg_update_on_view_will_disappear(page_base_t* self)
{


}
static void msg_update_on_view_did_disappear(page_base_t* self)
{


}
static void msg_update_on_view_did_unload(page_base_t* self)
{
    ui_msg_update_destroy(self->root);

}

static page_vtable_t page_msg_update_param = {

    .on_custom_attr_config  = msg_update_on_custom_attr_config,
    .on_view_load           = msg_update_on_view_load,
    .on_view_did_load       = msg_update_on_view_did_load,
    .on_view_will_appear    = msg_update_on_view_will_appear,
    .on_view_did_appear     = msg_update_on_view_did_appear,
    .on_view_will_disappear = msg_update_on_view_will_disappear,
    .on_view_did_disappear  = msg_update_on_view_did_disappear,
    .on_view_did_unload     = msg_update_on_view_did_unload,
};


page_vtable_t* lv_page_msg_update_create(void)
{
    return &page_msg_update_param;
}
